package com.vitabuybuy.tv.ui.auth

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityGuideBinding
import com.library.common.base.BaseActivity
import com.library.common.base.BaseViewModel
import com.library.common.ext.mmkv
import com.vitabuybuy.tv.ui.MainActivity
import com.library.common.util.MMKVContancts

import kotlinx.android.synthetic.main.activity_guide.*
import java.util.*

/**
 * 作者：SimGa
 * 日期：2019/5/8
 * 时间：11:57
 * 引导页
 */
class GuideActivity : BaseActivity<BaseViewModel, ActivityGuideBinding>() {
    /**
     * 引导页图片
     */
    private val pages = arrayOf(R.drawable.splash_1, R.drawable.splash_2, R.drawable.splash_3)
    private var viewList = ArrayList<View>()
    override val layoutId: Int
        get() = R.layout.activity_guide

    override val isUseBaseTitleBar: Boolean
        get() = false

    override fun initView(savedInstanceState: Bundle?) {
        val adapter = GuidePageAdapter()
        view_pager.adapter = adapter
        for (page in pages) {
            val iv = ImageView(mContext)
            iv.setImageResource(page)
            val params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
            iv.layoutParams = params
            iv.scaleType = ImageView.ScaleType.CENTER_CROP//图片按等比缩放
            viewList.add(iv)
        }
    }

    private inner class GuidePageAdapter : PagerAdapter() {
        override fun getCount(): Int {
            return pages.size
        }

        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return view === `object`
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            container.removeView(viewList[position])//删除页卡
        }

        override fun getItemPosition(`object`: Any): Int {
            return super.getItemPosition(`object`)
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            container.addView(viewList[position])
            return viewList[position]
        }
    }

    override fun eventHandling() {
        view_pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
            override fun onPageSelected(position: Int) {
                if (position == pages.size - 1) {//最后一页
                    btn_confirm.visibility = View.VISIBLE
                } else {
                    btn_confirm.visibility = View.GONE
                }
            }
            override fun onPageScrollStateChanged(state: Int) {}
        })
        btn_confirm.setOnClickListener {
             mmkv.encode(MMKVContancts.IS_FIRSH_IN, false)
            MainActivity.openActivity(mContext)
            finish()
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*,*>) {
            mContext.startActivity(null, GuideActivity::class.java)
        }
    }
}
